package edu.towson.cis.cosc490.jpanne1.lab3;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ToDoListActivity extends Activity implements OnClickListener, OnLongClickListener{
	Button m_launchAddActivityButton;
	LinearLayout m_itemListView;
	int darkOrLight;
	ArrayList<String> itemList = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initLayout();
		m_launchAddActivityButton.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_to_do_list, menu);
		return true;
	}
	
	private void initLayout(){
		m_launchAddActivityButton = (Button) this.findViewById(R.id.launchAddItemActivityButton);
		m_itemListView = (LinearLayout) this.findViewById(R.id.toDoListView);
		updateLayout();
	}
	
	private void updateLayout(){
		m_itemListView.removeAllViews();
		darkOrLight = 0;
		for(String i : itemList){
			TextView newItemTextView = new TextView(this);
			if(darkOrLight == 0){
				newItemTextView.setBackgroundColor(getResources().getColor(R.color.light));
				darkOrLight = 1;
			} else{
				newItemTextView.setBackgroundColor(getResources().getColor(R.color.dark));
				darkOrLight = 0;
			}
			newItemTextView.setText(i);
			newItemTextView.setTextColor(getResources().getColor(R.color.lightText));
			newItemTextView.setOnLongClickListener(this);
			m_itemListView.addView(newItemTextView);
		}
	}

	@Override
	public void onClick(View v) {
		Intent addItemIntent = new Intent(this, AddItemActivity.class);
		startActivityForResult(addItemIntent, 1);

	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent returnedIntent) {
		if (requestCode == 1) {
		     if(resultCode == RESULT_OK){
		    	 itemList.add(returnedIntent.getStringExtra("result"));
		     }
		}
		updateLayout();
	}
	
	public boolean onLongClick(View v){
		TextView tv = (TextView) v;
		String message = tv.getText().toString();
		Intent smsIntent = new Intent(Intent.ACTION_VIEW);
		smsIntent.putExtra("sms_body", "TO-DO List Item: "+ message);
		smsIntent.putExtra("address", "");
		smsIntent.setType("vnd.android-dir/mms-sms");
		startActivity(smsIntent);
		return false;
	}
}
